<?php
/**
 * 
 * +------------------------------------------------------------+
 * @category Page
 * +------------------------------------------------------------+
 * 根据自定义规则生成分页
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.judianyun.com 2013
 * @version 1.0
 *
 * Modified at : 2013-2-25 15:52:15
 *
 */
class Pager extends Base {
	
    ///分页显示格式%normal
	private $_format = '%total%first%prev%list%next%last%normal';
	
	///分页变量名
	private $_var = 'p';
	
	///数据总条数
	private $_count;
	
	///每次显示几条
	private $_listnum = 15;
	
	///分栏数目
	private $_column = 5;
	
	///分页a链接标签自定义class
	private $_className = array();
		
	private $_config = array(
		'record' => '条',
		'page' => '',
		'prev' => '上页',
		'next' => '下页',
		'first'=> '首页',
		'last' => '末页',
		'wrap' => ''
	);
	
	private $_urlfunc = '';
	
	/**
	 * 
	 * +------------------------------------------------------------+
	 * @name __construct
	 * +------------------------------------------------------------+
	 * 构造函数
	 * +------------------------------------------------------------+
	 * 
	 * @param int $count  数据总条数
	 * @param int $list	     每页显示条数
	 * @param int $column 分栏数
	 *
	 */
	public function __construct($count, $list = 15, $column = 5){
		parent::__construct();
		$this->_count = $count;
		$this->_listnum = (int)$list>0 ? (int) $list : 15;
		$this->_column = $column;
	}
	
	//添加className
	public function addClassName($className){
		$this->_className[] = $className;
		
		return $this; 
	}
	
	public function set_var_name($var_name){
		$this->_var = $var_name;
		
		return $this;
	}
	
	//设置显示格式
	public function format($format){
		if(!empty($format)) $this->_format = $format;
		
		return $this;
	}
	
	public function config($config){
		if (!empty($config)){
			$this->_config = extend($this->_config, $config);
		}
		
		return $this;
	}
	
	private $_tags = array(
	    'prev'     => '<a title="上一页" class="PAGER_PREV %class" href="%url">%text</a>',
	    'next'     => '<a title="下一页" class="PAGER_NEXT %class" href="%url">%text</a>',
	    'first'    => '<a title="第一页" class="PAGER_FIRST %class" href="%url">%text</a>',
	    'last'     => '<a title="最后一页" class="PAGER_LAST %class" href="%url">%text</a>',
	    'now'      => '<a class="PAGER_CURRENT">%text</a>',
	    'list'     => '<a class="PAGER_ITEM %class" href="%url">%text</a>',
	    'input'    => '<input %event max="%url" value="%text" placeholder="输入页码" class="PAGER_NORMAL">',
	    'record'   => '<span class="PAGER_TOTAL">%text</span>'
	);
	
	public function set_tag($key, $html){
	    if (isset($this->_tags[$key])){
	        $this->_tags[$key] = $html;
	    }
	    return $this;
	}
	
	private function _tag($key, $url, $text, $event = ''){
	    if (isset($this->_tags[$key])){
	        static $className = null,$prefix = null,$subfix = null;
	        if ($className === null){
	            $className = empty($this->_className) ? '' : ' ' . implode(' ', $this->_className);
	            $prefix = $this->_config['wrap'] != '' ? '<' . $this->_config['wrap'] . '>' : '';
	            $subfix = $this->_config['wrap'] != '' ? '</' . $this->_config['wrap'] . '>' : '';
	        }
	        $string = $prefix . $this->_tags[$key] . $subfix;
	        return str_replace(array('%class', '%url', '%text', '%event'), array($className, $url, $text, $event), $string);
	    }else{
	        return '';
	    }
	}
	
	public function display($auto_redirect = true){
		$list = $this->_listnum;
		$count = $this->_count;
		if ($count <= $list) return;

		$var = $this->_var;
		$totalPages = ceil($count/$list); //总页数
		
		$nowPage = min($totalPages, max(1, (int) $this->get->$var)); //当前页;
		$prev = $nowPage > 1 ? $nowPage - 1 : 1;    					 //上一页
		$next = $nowPage < $totalPages ? $nowPage + 1 : $totalPages;   //下一页
		
		$prevHtml = $nowPage <= 1 ? '' : $this->_tag('prev', $this->_url($prev), $this->_config['prev']);
		$nextHtml  = $nowPage >= $totalPages ? '' : $this->_tag('next', $this->_url($next), $this->_config['next']);
		
		$firstHtml = $nowPage > 1 ? $this->_tag('first', $this->_url(1), $this->_config['first']) : '';
		$lastHtml  = $nowPage < $totalPages ? $this->_tag('first', $this->_url($totalPages), $this->_config['last']) : '';
		
		$listHtml = '';
		
		if ($totalPages > $this->_column){
			$start = $nowPage <= 1 ? 1 : ($nowPage - 1);
			$end   = $start + $this->_column - 1;
			$end   = $end > $totalPages ? $totalPages : $end;
			$start = $end - $start < $this->_column ? $end - $this->_column + 1 : $start;
			$start = $start <=0 ? 1 : $start;
		}else{
			$start = 1;
			$end   = $totalPages;
		}
		
		
		for ($i = $start; $i <= $end; $i++){
		    if ($i != $nowPage){
		        $listHtml .= $this->_tag('list', $this->_url($i), $i);
		    }else{
		        $listHtml .= $this->_tag('now', $this->_url($i), $i);
		    }
		}
		
		//自定义显示页数
		$normalHtml = '';
		if ($totalPages > $this->_column && strpos($this->_format, '%normal') !== false){
		    $uri = str_replace('__P__', '', $this->_url('__P__'));
		    $event = '';
		    if($auto_redirect){
		        $event = 'onchange="var p=this.value!=\'\'?parseInt(this.value):0;if(p>0){p=p<=parseInt(this.max)?p:this.max;document.location.href=\''.$uri.'\'+p;}"';
		    }
		    $normalHtml = $this->_tag('input', $totalPages, $nowPage, $event);
		    $normalHtml .= '<input type="hidden" value="'.$uri.'">';
		}
		
		$record = $this->_tag('record', '', $count . $this->_config['record'] . ' / 共'.$totalPages.'页');
		
		$pageHtml = str_ireplace(
			array('%total', '%prev', '%next', '%list', '%first', '%last', '%normal'), 
		    array($record, $prevHtml, $nextHtml, $listHtml, $firstHtml, $lastHtml, $normalHtml), 
			$this->_format
		);
		unset($count, $totalPages, $nowPage, $prevHtml, $nextHtml, $listHtml, $firstHtml, $lastHtml, $normalHtml);
		return $pageHtml;
	}
	
	public function set_urlfunc($func){
	    $this->_urlfunc = $func;
	    
	    return $this;
	}
	
	private function _url($page){
	    if ($this->_urlfunc == '' || !function_exists($this->_urlfunc)){
    		static $params = null;
    		if ($params === null){
    			$params = extend($_GET, $_POST);
    		}
    		if (isset($params[$this->_var])){
    		    unset($params[$this->_var]);
    		}
    		$params[$this->_var] = $page;
    		
    		return url($this->_actionName, $this->_moduleName, $params);
	    }else{
	        $func = $this->_urlfunc;
	        return $func($page);
	    }
	}
}

?>